'use strict'

const {localStorageService} = require('../service/db/localStorage')
const {nativeTheme, app: ElectronApp} = require("electron")
const {loadConfig} = require('ee-core/config')
const {macOS, windows} = require('ee-core/utils/is')
const DEFAULT_UPDATER_LABEL = 'sendnotify'
/**
 * 应用服务（service层为单例）
 */
class AppService {
    // 读/写应用主题
    get appTheme() {
        //dark:黑色
        //white:白色
        const appTe = localStorageService.findOne('appTheme').value
        if (!appTe) {
            return this.#getAppDefaultTheme()
        }
        return appTe
    }

    set appTheme(value) {
        localStorageService.batchUpdateOrAdd([{
            name: 'appTheme',
            value: value
        }])
    }

    #getAppDefaultTheme() {
        if (nativeTheme.shouldUseDarkColors) {
            return 'dark'
        }
        return 'white'
    }

    // 读/写应用窗口背景色
    get appBackgroundColor() {
        // #2c2c32:dark
        // #f4f4f8:white
        const appBc = localStorageService.findOne('appBackgroundColor').value
        if (!appBc) {
            return this.#getDefaultBackgroundColor()
        }
        return appBc
    }

    set appBackgroundColor(value) {
        localStorageService.batchUpdateOrAdd([{
            name: 'appBackgroundColor',
            value: value
        }])
    }

    #getDefaultBackgroundColor() {
        if (nativeTheme.shouldUseDarkColors) {
            return '#2c2c32'
        }
        return '#f4f4f8'
    }

    //读/写应用主色
    get appPrimaryColor() {
        return localStorageService.findOne('appPrimaryColor').value || '#18a058'
    }

    set appPrimaryColor(value) {
        localStorageService.batchUpdateOrAdd([{
            name: 'appPrimaryColor',
            value: value
        }])
    }

    //读/写应用主hover色
    get appPrimaryHoverColor() {
        return localStorageService.findOne('appPrimaryHoverColor').value || '#36ad6a'
    }

    set appPrimaryHoverColor(value) {
        localStorageService.batchUpdateOrAdd([{
            name: 'appPrimaryHoverColor',
            value: value
        }])
    }

    //读/写应用主pressed色
    get appPrimaryPressedColor() {
        return localStorageService.findOne('appPrimaryPressedColor').value || '#0c7a43'
    }

    set appPrimaryPressedColor(value) {
        localStorageService.batchUpdateOrAdd([{
            name: 'appPrimaryPressedColor',
            value: value
        }])
    }

    //读/写应用主supplement色
    get appPrimarySupplColor() {
        return localStorageService.findOne('appPrimarySupplColor').value || '#2cb682'
    }

    set appPrimarySupplColor(value) {
        localStorageService.batchUpdateOrAdd([{
            name: 'appPrimarySupplColor',
            value: value
        }])
    }

    //读/写应用副色
    get appMinorPrimaryColor() {
        return localStorageService.findOne('appMinorPrimaryColor').value || '#647eff'
    }

    set appMinorPrimaryColor(value) {
        localStorageService.batchUpdateOrAdd([{
            name: 'appMinorPrimaryColor',
            value: value
        }])
    }

    //读/写应用窗口缩放比例
    get appZoomFactor() {
        let r = localStorageService.findOne('appZoomFactor').value || '1.0'
        return parseFloat(r)
    }

    set appZoomFactor(value) {
        localStorageService.batchUpdateOrAdd([{
            name: 'appZoomFactor',
            value: value
        }])
    }

    //读/写应用关闭窗口模式
    get appCloseWindow() {
        //minimize:最小化到托盘   quit:退出
        return localStorageService.findOne('appCloseWindow').value || 'minimize'
    }

    set appCloseWindow(value) {
        localStorageService.batchUpdateOrAdd([{
            name: 'appCloseWindow',
            value: value
        }])
    }

    //读/写应用国际化语言配置
    get appLanguageI18n() {
        //zh-CN:中文   en-US:英文(美国)
        return localStorageService.findOne('appLanguageI18n').value || 'zh-CN'
    }

    set appLanguageI18n(value) {
        localStorageService.batchUpdateOrAdd([{
            name: 'appLanguageI18n',
            value: value
        }])
    }

    //读/写主窗口是否初始化置顶配置
    get appMainPinned() {
        //true:已置顶 false:未置顶
        let str = localStorageService.findOne('appMainPinned').value || '0.0'
        return !!parseInt(str)
    }

    set appMainPinned(value) {
        localStorageService.batchUpdateOrAdd([{
            name: 'appMainPinned',
            value: +value
        }])
    }

    //读/写主窗口是否初始化最大化配置
    get appMainIsMaximized() {
        //true:最大化 false:正常化(非最大化)
        let str = localStorageService.findOne('appMainIsMaximized').value || '0.0'
        return !!parseInt(str)
    }

    set appMainIsMaximized(value) {
        localStorageService.batchUpdateOrAdd([{
            name: 'appMainIsMaximized',
            value: +value
        }])
    }

    //读/写应用更新配置
    get appAutoUpdates() {
        //autoupdate:自动下载更新 sendnotify:(默认)新版本发布时提醒我
        return localStorageService.findOne('appAutoUpdates').value || DEFAULT_UPDATER_LABEL
    }

    set appAutoUpdates(value) {
        localStorageService.batchUpdateOrAdd([{
            name: 'appAutoUpdates',
            value: value
        }])
    }

    //读/写应用下载目录
    get appDownloadsPath() {
        return localStorageService.findOne('appDownloadsPath').value
    }

    set appDownloadsPath(value) {
        localStorageService.batchUpdateOrAdd([{
            name: 'appDownloadsPath',
            value: value
        }])
        ElectronApp.setPath('downloads', value)
    }

    //读/写是否开启启动
    get loginPowerOn() {
        if (macOS() || windows()) {
            const {openAtLogin} = ElectronApp.getLoginItemSettings()
            return openAtLogin
        }
        return true
    }

    set loginPowerOn(value) {
        if (macOS() || windows()) {
            ElectronApp.setLoginItemSettings({
                openAtLogin: Boolean(value)
            })
        }
    }

    //读/写所有应用配置项
    get attributes() {
        const appConfig = localStorageService.findAll()
        //默认主题
        if (!appConfig.appTheme) {
            appConfig.appTheme = this.#getAppDefaultTheme()
        }
        //默认背景
        if (!appConfig.appBackgroundColor) {
            appConfig.appBackgroundColor = this.#getDefaultBackgroundColor()
        }
        //默认更新配置
        if (!appConfig.appAutoUpdates) {
            appConfig.appAutoUpdates = DEFAULT_UPDATER_LABEL
        }
        //应用是否强制更新
        const autoUpdater = loadConfig().autoUpdater
        appConfig._systemUpdaterForce = autoUpdater.force
        //默认下载目录
        if (!appConfig.appDownloadsPath) {
            appConfig.appDownloadsPath = ElectronApp.getPath('downloads')
        }
        //过滤
        appConfig.appMainPinned = !!parseInt(appConfig.appMainPinned || '0.0')
        appConfig.appMainIsMaximized = !!parseInt(appConfig.appMainIsMaximized || '0.0')
        appConfig.appZoomFactor = parseFloat(appConfig.appZoomFactor || '1.0')
        //添加
        appConfig.loginPowerOn = this.loginPowerOn
        return appConfig
    }

    set attributes(values) {
        let data = []
        for (let name in values) {
            switch (name) {
                case 'appDownloadsPath':
                    //下载目录设置
                    ElectronApp.setPath('downloads', values[name])
                    break
                case 'loginPowerOn':
                    //开启自启设置
                    this.loginPowerOn = values[name]
                    continue
            }
            data.push({
                name: name,
                value: values[name]
            })
        }
        if (data.length > 0) {
            localStorageService.batchUpdateOrAdd(data)
        }
    }
}

AppService.toString = () => '[class AppService]'
module.exports = {
    AppService,
    appService: new AppService()
}